//
//  FavoriteViewController.m
//  Shanghai Cuisine
//
//  Created by iOS Two on 14-9-12.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "FavoriteViewController.h"
#import "myData.h"
#import "myObj.h"
#import "UIImageView+MJWebCache.h"
#import "FavoriteDetailViewController.h"

@interface FavoriteViewController ()

@end

@implementation FavoriteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {
    [MobClick beginLogPageView:@"FavoriteViewController"];
}
-(void)viewDidAppear:(BOOL)animated {
    [MobClick endLogPageView:@"FavoriteViewController"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getData];
    UIBarButtonItem* backBtn = [[UIBarButtonItem alloc]init];
    backBtn.title = @"返回";
    self.navigationItem.backBarButtonItem = backBtn;
    
    UILabel *titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 44)];
    titelLabel.text = @"菜式收藏";
//    titelLabel.textAlignment = NSTextAlignmentCenter;
    titelLabel.font = [UIFont systemFontOfSize:19];
    titelLabel.textColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationItem.titleView = titelLabel;
    // Do any additional setup after loading the view.
}
-(void)creatController {
    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, kHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.rowHeight = 107;
    [self.view addSubview:table];
}
-(void)getData {
    _name = [[NSMutableArray alloc]init];
    _imgUrl = [[NSMutableArray alloc]init];
    _tips = [[NSMutableArray alloc]init];
    _introduction = [[NSMutableArray alloc]init];
    _bigImg = [[NSMutableArray alloc]init];
    
    myData *mydata=[[myData alloc]init];
    [mydata initData];
    NSArray *array = [mydata selectData];
    if (array.count == 0) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, kHeight)];
        label.backgroundColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.center = self.view.center;
        label.font = [UIFont systemFontOfSize:20];
        label.text = @"暂无收藏";
        [self.view addSubview:label];
    } else {
        for (int i = 0; i < array.count; i++) {
            myObj *obj = [array objectAtIndex:i];
            [_name addObject:obj.nameString];
            [_imgUrl addObject:obj.smallImg];
            [_tips addObject:obj.tips];
            [_introduction addObject:obj.introduction];
            [_bigImg addObject:obj.passNum];
        }
        [self creatController];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _imgUrl.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(5, 7, 125, 94)];
    [image setImageURLStr:[_imgUrl objectAtIndex:indexPath.row] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
    [cell.contentView addSubview:image];
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(154, 43, 100, 20)];
    name.text = [_name objectAtIndex:indexPath.row];
    name.font = [UIFont systemFontOfSize:18];
    [cell.contentView addSubview:name];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FavoriteDetailViewController *favor = [[FavoriteDetailViewController alloc]init];
    favor.dishName = [_name objectAtIndex:indexPath.row];
    favor.introduction = [_introduction objectAtIndex:indexPath.row];
    favor.tips = [_tips objectAtIndex:indexPath.row];
    favor.bigImg = [_bigImg objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:favor animated:YES];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        myData *mydata=[[myData alloc]init];
        [mydata initData];
        [mydata deleteTestList:[_name objectAtIndex:indexPath.row] tableName:@"dishName"];
        [mydata deleteTestList:[_name objectAtIndex:indexPath.row] tableName:@"howToDo"];
        [mydata deleteTestList:[_name objectAtIndex:indexPath.row] tableName:@"mainMaterial"];
        [mydata deleteTestList:[_name objectAtIndex:indexPath.row] tableName:@"auxiliaryMaterial"];
        
        [_name removeObjectAtIndex:indexPath.row];
        [_imgUrl removeObjectAtIndex:indexPath.row];
        
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [tableView endUpdates];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
